﻿// -----------------------------------------------------------------------
// <copyright file="ConfigManager.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using NetFramework.Configuration.ConfigSource;
using NetFramework.Diagnostics;
using System.Configuration;
namespace NetFramework.Configuration
{

    /// <summary>
    /// 配置管理。
    /// </summary>
    public static class ConfigManager
    {
        #region [ Properties ]

        /// <summary>
        /// 
        /// </summary>
        public static IConfigSource XmlConfig
        {
            get { return XMLConfigSource.Current; }
        }

        /// <summary>
        /// 
        /// </summary>
        public static IConfigSource DataSetConfig
        {
            get { return GetConfigSource(ConfigSourceType.DataSetConfig); }
        }

        /// <summary>
        /// 
        /// </summary>
        public static IConfigSource DotNetConfig
        {
            get { return DotNetConfigSource.Current; }
        }
        #endregion

        /// <summary>
        /// 获取默认配置数据源。
        /// </summary>
        /// <returns>配置源。</returns>
        public static IConfigSource GetConfigSource()
        {
            return GetConfigSource(ConfigSourceType.XmlConfig);
        }

        /// <summary>
        /// 获取默认配置数据源。
        /// </summary>
        /// <param name="configSourceType"></param>
        /// <returns></returns>
        public static IConfigSource GetConfigSource(ConfigSourceType configSourceType)
        {
            IConfigSource configSource = null;
            switch (configSourceType)
            {
                case ConfigSourceType.DotNetConfig:
                    configSource = DotNetConfigSource.Current;
                    break;
                case ConfigSourceType.XmlConfig:
                    configSource = XMLConfigSource.Current;
                    break;
                default:
                    configSource = Create(configSourceType.ToString());
                    break;
            }

            return configSource;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static IConfigSource Create(string name)
        {
            Ensure.ArgumentNotNull("name", name);

            //ConfigSettings configSettings = FrameworkConfig.GetConfig<ConfigSettings>();
            //ConfigProviderMapping providerMapping = configSettings.GetProviderMapping(name);
            //IConfigSource configSource = MethodCaller.CreateInstance(providerMapping.Type) as IConfigSource;
            //if (configSource == null)
            //{
            //    ConfigThrowHelper.ThrowConfigException(SR.ConfigSourceNotFound, name);
            //}

            //return configSource;
            return null;
        }

        /// <summary>
        /// 获取配置。
        /// </summary>
        /// <typeparam name="T">配置对象类型。</typeparam>
        /// <returns>配置。</returns>
        public static T GetConfig<T>() where T : class, new()
        {
            return GetConfigSource().GetConfig<T>();
        }

        public static string AppSetting(string key)
        {
            string result = ConfigurationManager.AppSettings[key];
            if (string.IsNullOrEmpty(result))
                result = string.Empty;

            return result;
        }

        public static T AppSetting<T>(string key, T defaultValue)
        {
            string result = ConfigurationManager.AppSettings[key];
            if (string.IsNullOrEmpty(result))
            {
                return defaultValue;
            }
            else
            {
                return StringConvertTo.ConvertTo<T>(result);
            }
        }
    }
}
